const {wrapper}=require('../middleWare/wrapper')

const {
    NAME_AND_PASSWORD_IS_REQUIRED,
    TOKEN_IS_REQURIED,
    NAME_REQURIED,
    RECHARGE_ERROR,
    PASSWORD_ERROR,
    USER_ERROR,
    TOKEN_IS_ERROR,
    AMOUNT_IS_REQUIRED,
    AMOUNT_IS_ERROR,
    CODE_REQUIRED,
    CODE_ERROR,
    CODE_NULL,
    INFORMATION_NULL,
    ORDERSTART_ERROR,
    AMOUNT_NOT_ENOUGH,
    AVATAR_ERROR,
    WXCODE_REQUIRED
}=require('./errorType')

const errorHanlder=async(error,ctx)=>{
    let status=200
    let message="success"
    switch(error.message){
        case NAME_AND_PASSWORD_IS_REQUIRED:
            status=400
            message=NAME_AND_PASSWORD_IS_REQUIRED
            break;
        case USER_ERROR:
            status=404
            message=USER_ERROR
            break;
        case PASSWORD_ERROR:
            status=403
            message=PASSWORD_ERROR
            break;
        case NAME_REQURIED:
            status=404
            message=NAME_REQURIED
            break;
        case TOKEN_IS_REQURIED:
            status=401
            message=TOKEN_IS_REQURIED
            break;
        case TOKEN_IS_ERROR:
            status=401
            message=TOKEN_IS_ERROR
            break;
        case AMOUNT_IS_REQUIRED:
            status=400
            message=AMOUNT_IS_REQUIRED
            break;
        case AMOUNT_IS_ERROR:
            status=400
            message=AMOUNT_IS_ERROR
            break;
        case RECHARGE_ERROR:
            status=400
            message=RECHARGE_ERROR
            break;
        case CODE_REQUIRED:
            status=404
            message=CODE_REQUIRED
            break;
        case CODE_ERROR:
            status=404
            message=CODE_ERROR
            break;
        case CODE_NULL:
            status=404
            message=CODE_NULL
            break;
        case INFORMATION_NULL:
            status=404
            message=INFORMATION_NULL
            break;
        case ORDERSTART_ERROR:
            status=400
            message=ORDERSTART_ERROR
            break;
        case AMOUNT_NOT_ENOUGH:
            status=404
            message=AMOUNT_NOT_ENOUGH
            break;
        case AVATAR_ERROR:
            status=404
            message=AVATAR_ERROR
            break;
        case WXCODE_REQUIRED:
            status=400
            message=WXCODE_REQUIRED
            break;
        default:
            break;
    }
    ctx.status=status
    ctx.message=message
    ctx.body=message

    await wrapper(ctx)
}

module.exports=errorHanlder